package com.example.springboot_test.easyexcel.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.example.springboot_test.easyexcel.model.FreezeRowColModel;
import com.example.springboot_test.easyexcel.model.common.SheetModel;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义冻结行和列处理器
 *
 * @author xudongmaster
 */
public class CustomFreezeRowColHandler implements SheetWriteHandler {

    /**
     * sheet页名称列表
     */
    private List<String> sheetNameList;
    /**
     * 冻结行和列信息
     */
    private List<FreezeRowColModel> freezeList = new ArrayList<>();


    public CustomFreezeRowColHandler(List<FreezeRowColModel> freezeList) {
        if (CollUtil.isEmpty(freezeList)) {
            return;
        }
        this.freezeList = freezeList.stream().filter(x ->
                StrUtil.isNotBlank(x.getSheetName()) && x.getDisplayRowIndex() >= 0 && x.getDisplayColIndex() >= 0
                        && x.getFreezeColNum() >= 0 && x.getFreezeRowNum() >= 0)
                .collect(Collectors.toList());
        sheetNameList = this.freezeList.stream().map(SheetModel::getSheetName).distinct().collect(Collectors.toList());
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    /**
     * sheet页创建之后调用
     *
     * @param writeWorkbookHolder 文档对象
     * @param writeSheetHolder    sheet页对象
     */
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        //不需要冻结行列，或者当前sheet页不需要冻结行列
        if (CollUtil.isEmpty(freezeList) || !sheetNameList.contains(sheet.getSheetName())) {
            return;
        }
        //获取当前sheet的冻结行列
        List<FreezeRowColModel> sheetFreezeList = freezeList.stream().filter(x ->
                StrUtil.equals(x.getSheetName(), sheet.getSheetName())
        ).collect(Collectors.toList());
        //当前sheet页不需要冻结行列
        if (CollUtil.isEmpty(sheetFreezeList)) {
            return;
        }
        for (FreezeRowColModel freezeRowColModel : sheetFreezeList) {
            //冻结行数
            int freezeRowNum = freezeRowColModel.getFreezeRowNum();
            //冻结列数
            int freezeColNum = freezeRowColModel.getFreezeColNum();
            //显示行号
            int displayRowIndex = freezeRowColModel.getDisplayRowIndex();
            //显示行索引不能为冻结行
            if (displayRowIndex < freezeRowNum) {
                displayRowIndex = freezeRowNum;
            }
            //显示列号
            int displayColIndex = freezeRowColModel.getDisplayColIndex();
            //显示列索引不能为冻结列
            if (displayColIndex < freezeColNum) {
                displayColIndex = freezeColNum;
            }
            sheet.createFreezePane(freezeColNum, freezeRowNum, displayColIndex, displayRowIndex);
        }
        //删除冻结行列信息
        freezeList.removeAll(sheetFreezeList);
        sheetNameList = freezeList.stream().map(SheetModel::getSheetName).distinct().collect(Collectors.toList());
    }
}